//
//  CZWelcomeViewController.swift
//  weibo123456
//
//  Created by 露露 on 16/6/13.
//  Copyright © 2016年 lulu. All rights reserved.
//

import UIKit

class CZWelcomeViewController: UIViewController {
    // MARK: - 属性
    /// 头像底部约束, 在初始化的时候确定不了约束,在 prepareUI 里面才能确定
    private var iconViewBottomCon: NSLayoutConstraint?
    


    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
    
    
    }
    // view看的到的时候做动画
    override func viewDidAppear(animated: Bool){
        
        super.viewDidAppear(animated)
        // 头像做动画,修改头像底部约束的值,往上是负数
        iconViewBottomCon?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        // 带弹簧效果
        // usingSpringWithDamping: 弹簧的效果, 范围是0 - 1, 越小越明显
        // initialSpringVelocity: 初始速度
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 动画
            // 闭包里面需要加self
            self.view.layoutIfNeeded()
        }) { (_) -> Void in
            
//            // 将label渐变出来
              self.welcomeLabel.alpha = 0
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                self.welcomeLabel.alpha = 1
                self.welcomeLabel.hidden = false
                }, completion: { (_) -> Void in
                    // _ 表示占位,对这个参数不关心
                    print("所有动画完成")
                    // 切换界面
                    (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
            })
        }
        
        
        
        
        
        
        
    }
    // MARK: - 准备UI
    private func prepareUI(){
        // 1.添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
         // 2.添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        // 背景,填充父控件
        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 头像,水平和父控件重合
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 垂直方向头像的底部和父控件的底部重合
        iconViewBottomCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewBottomCon!)
        
        // 欢迎的label,参照头像
        // 水平方向,欢迎label水平和头像 centerX重合
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 垂直方欢迎label水平和头像距离头像底部16
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))

    }
    // MARK: - 懒加载
    /// 背景
   

    private   lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
   // 头像
    private lazy var iconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named:"avatar_default_big"))
        
        // 将图片切成圆的
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
    
        return imageView
        
    }()
    // welcome label
    private lazy var welcomeLabel : UILabel = {
        let lebel = UILabel()
        lebel.text = "欢迎归来"
        // 设置文字大小
        lebel.font = UIFont.systemFontOfSize(14)
//        // 隐藏label
//        lebel.hidden = true
//        
        return lebel
    }()
}
